package Server.BusinessObjects;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import Server.DataAccess.ConnectJDBCMySQL;

public class User {


	private int id;
	private String name;
	private String password;
	private boolean isAdmin;

	public User() {

	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public boolean isAdmin() {
		return isAdmin;
	}

	public void setAdmin(boolean isAdmin) {
		this.isAdmin = isAdmin;
	}

	

	public User(ResultSet rs) throws SQLException {
		this.setId(rs.getInt("Id"));
		this.setName(rs.getString("Name"));
		this.setPassword(rs.getString("Password"));
		this.setAdmin(rs.getBoolean("IsAdmin"));
	}

	private static List<User> getList(PreparedStatement ps)
			throws SQLException {
		List<User> output = new ArrayList<User>();

		try {
			ResultSet rs = ps.executeQuery();

			while (rs.next())
				output.add(new User(rs));

			rs.close();
		} finally {
			Connection con = ps.getConnection();
			if (ps != null)
				ps.close();

			if (con != null)
				con.close();

		}

		return output;
	}

	public static User authenticateUser(String username, String password)
			throws SQLException {
		String query = "Select * from users where name = ? and password = ?";
		PreparedStatement ps = ConnectJDBCMySQL.getConnection()
				.prepareStatement(query);
		ps.setString(1, username);
		ps.setString(2, password);

		List<User> list = User.getList(ps);

		return list.size() == 1 ? list.get(0) : null;
	}

	public static User getUser(int id) throws SQLException {
		String query = "Select * from users where id = ?";
		PreparedStatement ps = ConnectJDBCMySQL.getConnection()
				.prepareStatement(query);
		ps.setInt(1, id);

		List<User> list = User.getList(ps);

		return list.size() == 1 ? list.get(0) : null;
	}

	public static User getUser(String name) throws SQLException {
		String query = "Select * from users where name = ?";
		PreparedStatement ps = ConnectJDBCMySQL.getConnection()
				.prepareStatement(query);
		ps.setString(1, name);

		List<User> list = User.getList(ps);

		return list.size() == 1 ? list.get(0) : null;
	}
	
	public static void insertPlayer(String username, String password, boolean isAdmin)
			throws SQLException {
		Connection dbCon = ConnectJDBCMySQL.getConnection();

		String insert = "INSERT INTO users (name, password, isAdmin) VALUES (?,?,?)";
		PreparedStatement preparedStatement = dbCon.prepareStatement(insert);
		preparedStatement.setString(1, username);
		preparedStatement.setString(2, password);
		preparedStatement.setBoolean(3, isAdmin);
		preparedStatement.execute();
		// autocommit is on by default
		dbCon.close();
	}
	
	public static void changePassword(int userId, String newPassword)
			throws SQLException {
		Connection dbCon = ConnectJDBCMySQL.getConnection();

		String insert = "UPDATE users SET password=? WHERE id=?";
		PreparedStatement preparedStatement = dbCon.prepareStatement(insert);
		preparedStatement.setString(1, newPassword);
		preparedStatement.setInt(2, userId);
		
		preparedStatement.execute();
		// autocommit is on by default
		dbCon.close();
	}
	
	public static void changeUsername(int userId, String newUsername)
			throws SQLException {
		Connection dbCon = ConnectJDBCMySQL.getConnection();

		String insert = "UPDATE users SET name=? WHERE id=?";
		PreparedStatement preparedStatement = dbCon.prepareStatement(insert);
		preparedStatement.setString(1, newUsername);
		preparedStatement.setInt(2, userId);
		
		preparedStatement.execute();
		// autocommit is on by default
		dbCon.close();
	}
}